//
//  SectionIndexView.m
//  ImGuider
//
//  Created by 李陆涛 on 2017/10/19.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "SectionIndexView.h"

@interface SectionIndexView ()

@property (nonatomic, strong) NSArray *originArray;
@property (nonatomic, strong) NSMutableArray *datasource;
@property (nonatomic, strong) UIView *indexView;

@end

@implementation SectionIndexView

- (instancetype)initWithFrame:(CGRect)frame sectionTitles:(NSArray *)titles {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        _originArray = titles;
        
        _datasource = [NSMutableArray array];
        
        for (NSString *title in titles) {
            if (title) {
                
                if (![_datasource containsObject:title]) {
                    [_datasource addObject:title];
                }
            }
        }
        [self setupUIWithFrame:frame];
    }
    return self;
}


- (void)setupUIWithFrame:(CGRect)frame {
    
    CGFloat aIndexH = 20;
    CGFloat aIndexW = CGRectGetWidth(frame);
    
    CGFloat indexViewHeight = 20 * self.datasource.count;
    
    if (indexViewHeight > CGRectGetHeight(frame)) {
        indexViewHeight = CGRectGetHeight(frame);
        
        aIndexH = indexViewHeight / self.datasource.count;
    }
    
    self.indexView = [[UIView alloc] initWithFrame:CGRectMake(0, (CGRectGetHeight(frame) - indexViewHeight) / 2.0, aIndexW, indexViewHeight)];
    [self addSubview:self.indexView];
    
    
    for (int i = 0; i < self.datasource.count; i ++) {
        
        NSString *title = self.datasource[i];
        
        CGFloat y = aIndexH * i;
        
        UILabel *indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, y, aIndexW, aIndexH)];
        
        indexLabel.text = title;
        indexLabel.font = Font(12);
        indexLabel.textAlignment = NSTextAlignmentCenter;
        indexLabel.textColor = kThemeColor;
        [self.indexView addSubview:indexLabel];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    UITouch* touch = [touches anyObject];
    [self selectIndexWithTouch:touch];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    UITouch* touch = [touches anyObject];
    [self selectIndexWithTouch:touch];
}

- (void)selectIndexWithTouch:( UITouch* )touch {
    
    
    CGPoint point = [touch locationInView:self.indexView];
    
    if (![self.indexView.layer containsPoint:point]) {
        return;
    }
    
    CGFloat tapY = point.y;
    
    NSInteger index = tapY / CGRectGetHeight(self.indexView.frame) * self.datasource.count;
    
    
    if (self.datasource.count > index && index >= 0) {
        NSString * title = self.datasource[index];
        
        NSInteger myIndex = [self.originArray indexOfObject:title];
        
        if ([_delegate respondsToSelector:@selector(selectIndex:)]) {
            [_delegate selectIndex:myIndex];
        }
    }
}

@end
